<?php

namespace App\Http\Middleware;

use App\Libs\AudioTemplateConf;
use App\Libs\FormatResult;
use Closure;
use App\Models\InterfaceConfig;
use App\Libs\FormatResultErrors;

/**
 * 调用sign.verify处理，任务包含：
 * 1. 发送
 * 2. 实例化好gearman client,并全局单例化
 * 3. 生成全局的识别码uuid，并全局可获取化
 *
 * @author Administrator
 *
 */
class GearApiSign
{
    const BIZ_TYPES = [
        'merchant.minilogin' => 'assis.merchant.minilogin',
        'voicenotify.mini' => 'assis.voicenotify.mini',
        'voicenotify.fetch' => 'assis.voicenotify.fetch',
    ];
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $dataOri = $request->input('data', '{}');
        $data = json_decode($dataOri, true);

        $ret = new FormatResult($data);

        if(empty($data['biz_type']) || empty(self::BIZ_TYPES[$data['biz_type']])){
            $ret->setError('SIGN.BIZ_TYPE.INVALID');
            return json_encode(array(
                'data' => json_encode($ret->getData(), JSON_UNESCAPED_UNICODE),
            ), JSON_UNESCAPED_UNICODE);
        }

        $request['work_name'] = self::BIZ_TYPES[$data['biz_type']];
        return $next($request);

    }
}
